package com.zhengtoudai.base;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;

import com.zhengtoudai.R;
import com.zhengtoudai.injector.module.ActivityModule;
import com.zhengtoudai.ui.activity.MainActivity;
import com.zhengtoudai.util.AbActivityManager;
import com.zhengtoudai.util.StatusBarUtil;
import com.zhengtoudai.util.TitleUtil;
import com.zhengtoudai.view.SwipeBackLayout;
import com.zhengtoudai.view.statusView.ActivityStatusView;
import com.zhengtoudai.view.swipetoloadlayout.base.SwipeToLoadLayout;


/**
 * 作者：黑哥 on 2016/3/29 09:53
 */
public abstract class BaseActivity extends AppCompatActivity{
    protected String TAG = this.getClass().getSimpleName();
    protected Context context;
    //滑动关闭activity
    private SwipeBackLayout layout;

    //对外提供一个title的方法
    protected TitleUtil titleUtil;
    //无数据无网络加载中布局
    public ActivityStatusView status;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        context = this;
        AbActivityManager.getInstance().addActivity(this);
        titleUtil=new TitleUtil(this);
        initSwipeBack();

        //创建状态布局
        status=new ActivityStatusView(context,this);
        status.setTitleView(titleUtil.getToolbar());
        StatusBarUtil.StatusBarLightMode(this);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//锁定竖屏
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //锁定横屏
        initTitle();
        initView();
    }

    protected abstract void initTitle();

    protected abstract void initView();

    protected ActivityModule getActivityModule() {
        return new ActivityModule(this);
    }

    public void startActivity(Class<?> cls) {
        Intent intent = new Intent(context, cls);
        startActivity(intent);
    }

    public void startActivity(Bundle bundle, Class<?> cls) {
        Intent intent = new Intent(context, cls);

        intent.putExtras(bundle);
        startActivity(intent);
    }
    public void startActivityForResult(Bundle bundle,int requestCode ,Class<?> cls) {
        Intent intent = new Intent(context, cls);
        intent.putExtras(bundle);
        startActivityForResult(intent,requestCode);
    }

    public void startActivityForResult(int requestCode ,Class<?> cls) {
        Intent intent = new Intent(context, cls);
        startActivityForResult(intent,requestCode);
    }

    /**
     * 标示该activity是否可滑动退出,默认true
     */
    protected boolean mEnableSlidr = true;
    private void initSwipeBack() {
        if (mEnableSlidr) {
              layout = (SwipeBackLayout) LayoutInflater.from(this).inflate(
                    R.layout.swipeback, null);
            layout.attachToActivity(this);
        }

    }


    /**
     * 设置activity启动动画
     */
    @Override
    protected void onStart() {
        super.onStart();
        if (getClass() != MainActivity.class) {
            overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_left_out);
        }
    }

    /**
     * 设置activity关闭动画
     */
    @Override
    public void finish() {
        super.finish();
        if (getClass() != MainActivity.class)
            overridePendingTransition(R.anim.base_slide_left_in, R.anim.base_slide_right_out);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //HttpUtils.tagCancelRequest(this);
    }

    /**
     *
     * @param message  提示信息
     * @param btnNegative  取消按钮
     * @param btnPositive  确认按钮
     * @param positiveListener 确认按钮点击事件
     * @return
     */
    public AlertDialog.Builder showAlertDialog(String message, String btnNegative, String btnPositive, Dialog.OnClickListener positiveListener){
        final AlertDialog.Builder builder=new AlertDialog.Builder(context);
        builder.setTitle("温馨提示");
        builder.setMessage(message);
        builder.setNegativeButton(btnNegative, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setPositiveButton(btnPositive,positiveListener);
        builder.show();
        return  builder;
    }
    private ProgressDialog mProgressDialog;
    public ProgressDialog showProgressDialog(String message){
        if (mProgressDialog==null){
            mProgressDialog=new ProgressDialog(this);
            mProgressDialog.setCancelable(false);
            mProgressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (event.getAction()==KeyEvent.ACTION_DOWN&&keyCode==KeyEvent.KEYCODE_BACK){
                        dialog.dismiss();
                        return true;
                    }
                    return false;
                }
            });
        }
        mProgressDialog.setMessage(message);
        mProgressDialog.show();
        return mProgressDialog;
    }

    public void dismissProgressDialog(){
        if (mProgressDialog!=null){
            mProgressDialog.dismiss();
        }
    }
    protected void onComplete(SwipeToLoadLayout refresh){
        if (refresh!=null){
            if (refresh.isRefreshing()){
                refresh.setRefreshing(false);
            }else if (refresh.isLoadingMore()){
                refresh.setLoadingMore(false);
            }
        }

    }
    protected void autoLoading(final SwipeToLoadLayout refresh,RecyclerView swipeTarget){
        //自动上拉
        swipeTarget.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (!ViewCompat.canScrollVertically(recyclerView, 1)) {
                        refresh.setLoadingMore(true);
                    }
                }
            }
        });
    }
}
